package com.uphone.magicleafteacher;

import android.Manifest;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.uphone.magicleafteacher.base.BaseGActivity;
import com.uphone.magicleafteacher.base.OnPermissionCallBack;
import com.uphone.magicleafteacher.pro.fragment.home.HomeFragment;
import com.uphone.magicleafteacher.pro.fragment.message.MessageFragment;
import com.uphone.magicleafteacher.pro.fragment.mine.MineFragment;
import com.uphone.magicleafteacher.view.NoScrollViewPager;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseGActivity {
    @BindView(R.id.vp_main)
    NoScrollViewPager mVpMain;
    @BindView(R.id.bottom_navigation)
    BottomNavigationView bottomNavigation;
    private ViewPagerAdapter mViewPagerAdapter;
    List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void initBundle() {

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        mVpMain.setNoScroll(true);
        fragments.add(HomeFragment.newInstance(""));
        fragments.add(MessageFragment.newInstance(""));
        fragments.add(MineFragment.newInstance(""));


        //除去自带效果
        bottomNavigation.setItemIconTintList(null);


        mViewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragments);
        mVpMain.setAdapter(mViewPagerAdapter);
    }

    @Override
    protected void initTitle() {

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片、视频、音频选择结果回调
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    if (selectList != null && selectList.size() > 0) {
                        String path = selectList.get(0).getPath();
                        if (!TextUtils.isEmpty(path) && new File(path).exists()) {
                            if (fragments != null && fragments.size() > 2 && fragments.get(2) != null && fragments.get(2) instanceof MineFragment)
                                ((MineFragment) fragments.get(2)).uploadImgFile(new File(path));
                        }
                    }

                    break;
                default:
                    break;
            }
        }
    }

    @Override
    protected void initData() {

        getPermission(new OnPermissionCallBack() {
            @Override
            public void permissionPass(String[] permissions) {
                File file = new File("/storage/emulated/0/DCIM/Screenshots/Screenshot_2019-05-24-18-18-57-594_com.uphone.kingmall.png");
//                Map<String, RequestBody> mapRB = new HashMap<>();
//                mapRB.put("token", RequestBodyUtil.convertToRequestBody("d7f55d40ff168e4158b4833f37d51ae6"));
//                mapRB.put("uid", RequestBodyUtil.convertToRequestBody("8"));
//                MultipartBody.Part part = RequestBodyUtil.filesToMultipartBodyPart("file", file);
//                MyApp.apiService(ApiService.class)
//                        .userHightScoreUpload(mapRB, part)
//                        .compose(RxSchedulers.io_main())
//                        .doOnSubscribe(d -> showLoading())
//                        .doFinally(() -> closeLoading())
//                        .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from
//                                (MainActivity.this)))
//                        .subscribe(bean -> {
//                            LogUtils.e("bean:" + bean.string());
//                        }, throwable -> {
//                            // 异常
//                            LogUtils.e("throwable:" + throwable.toString());
//                            onFail();
//                        }, () -> {
//                            // 完成
//                        });
            }

            @Override
            public void permissionRefuse(String[] permissions) {

            }
        }, Manifest.permission.WRITE_EXTERNAL_STORAGE);


    }

    @Override
    protected void initListener() {
        bottomNavigation.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.action_home:
                    naviTab(0);
                    break;
                case R.id.action_project:
                    naviTab(1);
                    break;
                case R.id.action_system:
                    naviTab(2);
                    break;
                default:
                    break;
            }
            return true;
        });

    }

    /**
     * 点击Navigation切换Tab
     *
     * @param position tab下标
     */
    private void naviTab(int position) {
        mVpMain.setCurrentItem(position, true);
    }

    public class ViewPagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> mFragments;
        private List<String> listTitle = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager fm, List<Fragment> mFragments) {
            super(fm);
            this.mFragments = mFragments;
        }

        public ViewPagerAdapter(FragmentManager fm, List<Fragment> mFragments, List<String> listTitle) {
            super(fm);
            this.mFragments = mFragments;
            this.listTitle = listTitle;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return listTitle.get(position);
        }

        @Override
        public int getItemPosition(Object object) {
            return PagerAdapter.POSITION_NONE;
        }

        public void recreateItems(List<Fragment> fragmentList, List<String> titleList) {
            this.mFragments = fragmentList;
            this.listTitle = titleList;
            notifyDataSetChanged();
        }
    }
}
